/**
 * cn.com.oceansoft.application.config.MyBatisConfig
 *
 * @author chenw
 * @create 16/4/27.19:41
 * @email javacspring@hotmail.com
 */

package cn.com.oceansoft.application.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;

/**
 * @author chenw
 * @create 16/4/27 19:41
 * @email javacspring@gmail.com
 */
//@Configuration
//@Import(DataSourceConfig.class)
//@MapperScan(basePackages = "cn.com.oceansoft.modules")
public class MyBatisConfig {

    @Bean(name = "sqlSessionFactory")
//    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
//        sessionFactory.setMapperLocations();
        return sessionFactory.getObject();
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
//        scannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        scannerConfigurer.setAnnotationClass(Repository.class);
        scannerConfigurer.setBasePackage("cn.com.oceansoft.modules");
        return scannerConfigurer;
    }

}
